package m.jclecai.com.adapter.KSPagerAdapters;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import m.jclecai.com.R;
import m.jclecai.com.inter.IAdapterItemSelect;

/**
 * Created by hp on 2017/5/2.
 * 三同号
 */

public class BetThreeSameAdapter extends BaseAdapter implements View.OnClickListener {

    private static final String TAG = "三同号";
    private final Context context;
    private final IAdapterItemSelect i;
    private String[] threeSame;
    private boolean isClear;
    private int selectMode;
    private int randomNumber;
    private List<String> betParamete = new LinkedList<>(); //用户投注参数 快速选择
    Map<Integer, Boolean> selectMap = new HashMap<>(); //自选

    //用来存储ItemView的；
    private String[] mMissData=new String[]{};

    private boolean mShowMissData;
    private ListView mListView;

    public BetThreeSameAdapter(Context context, IAdapterItemSelect i) {
        this.context = context;
        this.i = i;
    }

    public void setData(String[] threeSame) {
        this.threeSame = threeSame;
    }

    public void clearSelect() {
        isClear = true;
    }

    //获取投注参数
    public List<String> getBetParamete() {
        for (Map.Entry<Integer, Boolean> entry : selectMap.entrySet()) {
            if (entry.getValue()) {
                betParamete.add(entry.getKey() + "");
            }
        }
        return betParamete;
    }

    public void setRandomNumber(int randomNumber) {
        //this.randomNumber = randomNumber;
        //isClear = true;
        //这里我们直接使用ListView来实现界面的ui刷新；
        clearAll();
        List<String> stringList = Arrays.asList(threeSame);
        int numPosition = stringList.indexOf(randomNumber + "");
        int holderPostion = numPosition / 3;
        int itemPostion = numPosition % 3;

        View view = mListView.getChildAt(holderPostion);
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        switch (itemPostion) {
            case 0:
                setSelected(viewHolder, viewHolder.tv_number_one, viewHolder.rl_one);
                break;
            case 1:
                setSelected(viewHolder, viewHolder.tv_number_two, viewHolder.rl_two);
                break;
            case 2:
                setSelected(viewHolder, viewHolder.tv_number_three, viewHolder.rl_three);
                break;
        }

    }

    public void clearAll() {

        for (int i = 0; i < mListView.getChildCount(); i++) {
            View view = mListView.getChildAt(i);
            ViewHolder holder = (ViewHolder) view.getTag();
            clearSelect(holder);
        }
        isClear = false;
        i.adapterItemSelect(selectMap);
    }

    public void setListView(ListView listView){
        mListView = listView;
    }

    @Override
    public int getCount() {
        return threeSame.length / 3 + 1;
    }

    @Override
    public Object getItem(int position) {
        return threeSame[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (true) {
            holder = new ViewHolder();
            holder.position = position;
            if (position == threeSame.length / 3) {
                convertView = View.inflate(context, R.layout.ks_item_three_same_bottom, null);
                holder.tv_three_same = (TextView) convertView.findViewById(R.id.tv_three_same);
                holder.tv_three_same.setOnClickListener(this);
                holder.tv_three_same.setTag(holder);
            } else {
                convertView = View.inflate(context, R.layout.ks_item_three_same, null);
                holder.tv_number_one = (TextView) convertView.findViewById(R.id.tv_number_one);
                holder.tv_number_two = (TextView) convertView.findViewById(R.id.tv_number_two);
                holder.tv_number_three = (TextView) convertView.findViewById(R.id.tv_number_three);

                holder.tv_miss_data_one = ((TextView) convertView.findViewById(R.id.tv_miss_data_one));
                holder.tv_miss_data_two = ((TextView) convertView.findViewById(R.id.tv_miss_data_two));
                holder.tv_miss_data_three = ((TextView) convertView.findViewById(R.id.tv_miss_data_three));

                holder.rl_one = (RelativeLayout) convertView.findViewById(R.id.rl_one);
                holder.rl_one.setOnClickListener(this);
                holder.rl_two = (RelativeLayout) convertView.findViewById(R.id.rl_two);
                holder.rl_two.setOnClickListener(this);
                holder.rl_three = (RelativeLayout) convertView.findViewById(R.id.rl_three);
                holder.rl_three.setOnClickListener(this);
                holder.rl_one.setTag(holder);
                holder.rl_two.setTag(holder);
                holder.rl_three.setTag(holder);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //设置数据
        switch (position) {
            case 0:
                holder.tv_number_one.setText(threeSame[0]);
                holder.tv_number_two.setText(threeSame[1]);
                holder.tv_number_three.setText(threeSame[2]);

                if (mMissData.length >= 3) {
                    //这里我们要做的一个操作就是赋值
                    holder.tv_miss_data_one.setText(mMissData[0]);
                    holder.tv_miss_data_two.setText(mMissData[1]);
                    holder.tv_miss_data_three.setText(mMissData[2]);
                }

                break;
            case 1:
                holder.tv_number_one.setText(threeSame[3]);
                holder.tv_number_two.setText(threeSame[4]);
                holder.tv_number_three.setText(threeSame[5]);

                if (mMissData.length >= 6) {
                    //这里我们要做的一个操作就是赋值
                    holder.tv_miss_data_one.setText(mMissData[4]);
                    holder.tv_miss_data_two.setText(mMissData[5]);
                    holder.tv_miss_data_three.setText(mMissData[6]);
                }
                break;
        }
        if (isClear) {
            clearSelect(holder);
        }
        if(position<getCount()-1){
            if (isShowMissData()) {
                holder.tv_miss_data_one.setVisibility(View.VISIBLE);
                holder.tv_miss_data_two.setVisibility(View.VISIBLE);
                holder.tv_miss_data_three.setVisibility(View.VISIBLE);
            } else {
                holder.tv_miss_data_one.setVisibility(View.INVISIBLE);
                holder.tv_miss_data_two.setVisibility(View.INVISIBLE);
                holder.tv_miss_data_three.setVisibility(View.INVISIBLE);
            }
        }

        //随机
        if (randomNumber != 0 && position < threeSame.length / 3) {

            holder.rl_one.setBackgroundResource(R.drawable.shape_box_ks_up);
            holder.rl_two.setBackgroundResource(R.drawable.shape_box_ks_up);
            holder.rl_three.setBackgroundResource(R.drawable.shape_box_ks_up);
            if (randomNumber == Integer.parseInt(holder.tv_number_one.getText().toString())) {
                speedinessSelected(holder, holder.tv_number_one, holder.rl_one);
            } else if (randomNumber == Integer.parseInt(holder.tv_number_two.getText().toString())) {
                speedinessSelected(holder, holder.tv_number_two, holder.rl_two);
            } else if (randomNumber == Integer.parseInt(holder.tv_number_three.getText().toString())) {
                speedinessSelected(holder, holder.tv_number_three, holder.rl_three);
            }

            if (position == threeSame.length / 3 - 1) {    //这里表示的意思最后一个条目的时候，我们是需要还原回去；
                randomNumber = 0;
                isClear = false;
            }
        }

        return convertView;
    }

    //快速选号
    private void speedinessSelected(ViewHolder holder, TextView tv, RelativeLayout rl) {
        key = 0;
        key = Integer.parseInt(tv.getText().toString());
        rl.setBackgroundResource(R.drawable.shape_box_ks_down);
        tv.setTextColor(tv.getResources().getColor(R.color.yello));
        holder.setSelected(key, true);
    }

    int key;

    private void setSelected(ViewHolder holder, TextView tv, RelativeLayout rl) {
        key = 0;
        key = Integer.parseInt(tv.getText().toString());
        if (holder.isSelected(key)) {
            rl.setBackgroundResource(R.drawable.shape_box_ks_up);
            //设置字体的颜色
            tv.setTextColor(tv.getResources().getColor(R.color.white));
            holder.removeSelected(key);
        } else {
            rl.setBackgroundResource(R.drawable.shape_box_ks_down);
            tv.setTextColor(tv.getResources().getColor(R.color.yello));
            holder.setSelected(key, true);

        }
    }


    private void clearSelect(ViewHolder holder) {
        if (holder.position == 0) {
            selectMap.clear();
            betParamete.clear();
        }
        if (holder.position < threeSame.length / 3) {
            holder.rl_one.setBackgroundResource(R.drawable.shape_box_ks_up);
            holder.rl_two.setBackgroundResource(R.drawable.shape_box_ks_up);
            holder.rl_three.setBackgroundResource(R.drawable.shape_box_ks_up);

            holder.tv_number_one.setTextColor(holder.rl_one.getResources().getColor(R.color.white));
            holder.tv_number_two.setTextColor(holder.rl_one.getResources().getColor(R.color.white));
            holder.tv_number_three.setTextColor(holder.rl_one.getResources().getColor(R.color.white));
        } else {
            clearSelect(holder.tv_three_same);
            isClear = false;
            i.adapterItemSelect(selectMap);
        }
    }

    //设置选中
    private void selectMode(TextView tv) {
       // tv.setBackgroundResource(R.color.yello);
        //这里我们要设置111 222 333 444来设置边界；
        ((ViewGroup) tv.getParent()).setBackgroundResource(R.drawable.shape_box_ks_down);
        tv.setTextColor(tv.getResources().getColor(R.color.yello));

    }

    //清除选中
    private void clearSelect(TextView tv) {
       //tv.getParent()
        ((ViewGroup) tv.getParent()).setBackgroundResource(R.drawable.shape_box_ks_up);
        tv.setTextColor(tv.getResources().getColor(R.color.white));
    }

    @Override
    public void onClick(View v) {
        ViewHolder holder = (ViewHolder) v.getTag();
        switch (v.getId()) {
            case R.id.tv_three_same:
                if (selectMode == 0) {
                    selectMode(holder.tv_three_same);
                    selectMode = 1;
                    holder.setSelected(9999, true);
                } else {
                    clearSelect(holder.tv_three_same);
                    selectMode = 0;
                    holder.removeSelected(9999);
                }
                break;
            case R.id.rl_one:
                setSelected(holder, holder.tv_number_one, holder.rl_one);
                break;
            case R.id.rl_two:
                setSelected(holder, holder.tv_number_two, holder.rl_two);
                break;
            case R.id.rl_three:
                setSelected(holder, holder.tv_number_three, holder.rl_three);
                break;
        }
       // notifyDataSetChanged();
    }

    public void addMissDataInfo(String missData){
        mMissData = missData.split(",");
        //这里我们要做的一个操作就是显示将数据添加到里面去；

        if (mListView.getChildCount() == 0) {
            notifyDataSetChanged();
        } else {
            if (mMissData.length >= 6) {
                for (int i = 0; i < mListView.getChildCount() - 1; i++) {
                    ViewHolder viewHolder = (ViewHolder) mListView.getChildAt(i).getTag();
                    //没一个条目有五个子view;
                    viewHolder.tv_miss_data_one.setText(mMissData[i * 3 + 0]);
                    viewHolder.tv_miss_data_two.setText(mMissData[i * 3 + 1]);
                    viewHolder.tv_miss_data_three.setText(mMissData[i * 3 + 2]);
                }
            } else {
                for (int i = 0; i < mListView.getChildCount() - 1; i++) {
                    ViewHolder viewHolder = (ViewHolder) mListView.getChildAt(i).getTag();
                    //没一个条目有五个子view;
                    viewHolder.tv_miss_data_one.setText("--");
                    viewHolder.tv_miss_data_two.setText("--");
                    viewHolder.tv_miss_data_three.setText("--");
                }
            }
        }
    }

    public boolean isShowMissData() {
        return mShowMissData;
    }

    public void showMissData(){
        mShowMissData=true;

        if(mListView.getChildCount()==0){
            notifyDataSetChanged();

        }else{
            ViewHolder holder0 = (ViewHolder) mListView.getChildAt(0).getTag();
            ViewHolder holder1 = (ViewHolder) mListView.getChildAt(1).getTag();


            holder0.tv_miss_data_one.setVisibility(View.VISIBLE);
            holder0.tv_miss_data_two.setVisibility(View.VISIBLE);
            holder0.tv_miss_data_three.setVisibility(View.VISIBLE);

            holder1.tv_miss_data_one.setVisibility(View.VISIBLE);
            holder1.tv_miss_data_two.setVisibility(View.VISIBLE);
            holder1.tv_miss_data_three.setVisibility(View.VISIBLE);

        }
    }
    public void hideMissData(){
        mShowMissData=false;
        int childCount = mListView.getChildCount();
        if(childCount==0){
            notifyDataSetChanged();
        }else{

            ViewHolder holder0 = (ViewHolder) mListView.getChildAt(0).getTag();
            ViewHolder holder1 = (ViewHolder) mListView.getChildAt(1).getTag();
            holder0.tv_miss_data_one.setVisibility(View.INVISIBLE);
            holder0.tv_miss_data_two.setVisibility(View.INVISIBLE);
            holder0.tv_miss_data_three.setVisibility(View.INVISIBLE);

            holder1.tv_miss_data_one.setVisibility(View.INVISIBLE);
            holder1.tv_miss_data_two.setVisibility(View.INVISIBLE);
            holder1.tv_miss_data_three.setVisibility(View.INVISIBLE);

        }
    }


    private class ViewHolder {
        TextView tv_number_one;
        TextView tv_number_two;
        TextView tv_number_three;

        TextView tv_miss_data_one;
        TextView tv_miss_data_two;
        TextView tv_miss_data_three;


        TextView tv_three_same;

        int position;

        RelativeLayout rl_one;
        RelativeLayout rl_two;
        RelativeLayout rl_three;

        public void setSelected(int key, boolean value) {
            selectMap.put(key, value);
            Log.i(TAG, "key=" + key);
            Log.i(TAG, "selectMap=" + selectMap.size());
            i.adapterItemSelect(selectMap);

        }

        public void removeSelected(int key) {
            selectMap.remove(key);
            Log.i(TAG, "删除后=" + selectMap.size());
            i.adapterItemSelect(selectMap);
        }

        public boolean isSelected(int key) {
            if (selectMap.get(key) == null) {
                return false;
            } else {
                return selectMap.get(key);
            }
        }
    }
}
